import datetime

import django.utils.timezone
from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator
from django.contrib.auth import get_user_model


class History(models.Model):
    number = models.IntegerField(primary_key=True, verbose_name='顺序')
    title = models.CharField(verbose_name='标题', max_length=100)
    content = models.TextField(verbose_name='内容', max_length=10000)

    def __str__(self):
        return f'第{self.number}篇：{self.title}'

    class Meta:
        verbose_name = '历史'
        verbose_name_plural = '历史'
        ordering = ('number', )


class Notice(models.Model):
    title = models.CharField(max_length=200, verbose_name='标题')
    content = models.TextField(verbose_name='内容', max_length=10000)
    time = models.DateTimeField(verbose_name='时间', auto_now=True)

    def __str__(self):
        return f'于{self.time.strftime("%Y年%m月%d日 %H时%M分%S秒")}发布的公告：{self.title}'

    class Meta:
        verbose_name = '公告'
        verbose_name_plural = '公告'
        ordering = ('time', )


class Advice(models.Model):
    title = models.CharField(max_length=200, verbose_name='标题')
    content = models.TextField(verbose_name='内容', max_length=10000)
    sender = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, verbose_name='发送者',
                               related_name='advice_sender')
    finished = models.BooleanField(verbose_name='已完成')

    def __str__(self):
        return f'{"已" if self.finished else "未"}完成的来自{self.sender}的建议：{self.title}'

    class Meta:
        verbose_name = '建议'
        verbose_name_plural = '建议'
        ordering = ('-finished', 'sender', 'title', 'content')


class Progress(models.Model):
    name = models.CharField(max_length=200, verbose_name='名称', primary_key=True)
    value = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(100)], verbose_name='进程值')
    start_date = models.DateField(default=django.utils.timezone.now, verbose_name='开始日期')

    def __str__(self):
        value = (self.value + 99) % 100 + 1
        return f'开始于{self.start_date.strftime("%Y年%m月%d日")}的完成{value}%的进程{self.name}'

    class Meta:
        verbose_name = '进程'
        verbose_name_plural = '进程'
        ordering = ('start_date', '-value', 'name')


class Message(models.Model):
    content = models.CharField(max_length=1000)
